package cn.kgc.count.controller;

import cn.kgc.count.service.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by teacher ZHANG on 2023/2/6
 */
@RestController
public class CountController {
    @Autowired
    private CountService countService;

    @PostMapping("/counts")
    public Integer addAddress(Integer articleId, String address) {
        return countService.addAddress(articleId, address);
    }

    @GetMapping("/counts")
    public List<Map<String, Integer>> listByVisitors(String articleIds, Integer topNum) {
        if (articleIds == null) {
            return countService.listByVisitors(topNum);
        }

        String[] array = articleIds.split(",");
        List<Integer> list = new ArrayList<>();

        for (String id : array) {
            list.add(Integer.parseInt(id));
        }

        return countService.listCounts(list);
    }

}
